package com.heng.compare;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 外部比较
 *
 * @author heng
 * @date 2022年01月28日07:29:45
 */
public class TestComparator {


    public static void main(String[] args) {
        Teach[] array = new Teach[4];
        array[0] = new Teach("张老师", 3);
        array[1] = new Teach("张老师", 2);
        array[2] = new Teach("张老师", 5);
        array[3] = new Teach("张老师", 4);

        //定义比较器
        TeachComparator teach = new TeachComparator();
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {
                if (teach.compare(array[j], array[j + 1]) > 0) {
                    Teach teachTemp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = teachTemp;
                }
            }
        }

        System.out.println(Arrays.toString(array));


    }


    static class Teach {
        String name;

        public Teach() {
        }

        int lever;

        public Teach(String name, int lever) {
            this.name = name;
            this.lever = lever;
        }

        @Override
        public String toString() {
            return "Teach{" + "name='" + name + '\'' + ", lever=" + lever + '}';
        }


    }

    static class TeachComparator implements Comparator {


        @Override
        public int compare(Object o1, Object o2) {
            Teach t1 = (Teach) o1;
            Teach t2 = (Teach) o2;
            return t1.lever - t2.lever;
        }
    }
}
